import { Tag } from 'antd';
import React from 'react';
import ex from 'umi/dist';

/**
 * 自定义打印
 * @param data 要打印的对象
 * @param msg
 */
export const log = (data: any, msg?: string) => {
  if (process.env.APP_LOG) {
    console.log({
      msg: msg ?? '打印变量',
      // data: JSON.stringify(JSON.parse(String(data)),null,2)
      data: data,
      time: Date().toString(),
    });
  }
};

/**
 * 字符串转rgba
 * @param str
 */
export const rgbaColorByString = (str: string) => {
  const rgbaList = str.split('-');
  if (rgbaList.length == 4) {
    return `rgba(${rgbaList[0]},${rgbaList[1]},${rgbaList[2]},${rgbaList[3]})`;
  }
  return 'rgba(0, 0, 0, 1)';
};

/**
 * 数组转bool
 * @param num
 */
export const numberToBool = (num?: number) => {
  if (num === undefined) {
    return false;
  }
  return num !== 0;
};

/**
 * bool值转0或者1
 * @param bool
 */
export const boolToNumOneOrZero = (bool: boolean) => {
  if (bool) {
    return 1;
  }
  return 0;
};

/**
 * 处理富文本里面图片的样式
 * @param html
 */
export const articleContentImage = (html: string) => {
  let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
    match = match
      .replace(/height="[^"]+"/gi, '')
      .replace(/height='[^']+'/gi, '');
    return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
    match = match
      .replace(/width:[^;]+;/gi, 'max-width:100%;')
      .replace(/width:[^;]+;/gi, 'max-width:100%;');
    return match;
  });
  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;"');
  newContent = newContent.replace(/\<ul>/gi, '<ul style="padding-left: 20px">');
  newContent = newContent.replace(/\<ol>/gi, '<ol style="padding-left: 20px">');
  return newContent;
};

/**
 * 新窗口打开链接
 * @param url
 */
export const openUrl = (url: string) => window.open(url);
